﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Entities;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class RolDAL : DBController
    {
        public RolDAL()
        { 
        
        }

        /// <summary>
        /// Funcion que devuelve los roles activos
        /// </summary>
        /// <returns></returns>
        public List<RolEntity> GetRoles()
        {
            try
            {
                List<RolEntity> roles = new List<RolEntity>();
                using (SqlConnection conn = this.OpenConn())
                {

                    SqlDataReader dr;

                    SqlCommand dCmd = new SqlCommand("MARAVILLA_SQL.spGetRoles", conn);

                    dCmd.CommandText = "MARAVILLA_SQL.spGetRoles";

                    dCmd.CommandType = CommandType.StoredProcedure;


                    // dCmd.ExecuteNonQuery();
                    dr = dCmd.ExecuteReader();
                    dCmd.Dispose();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            RolEntity rol = new RolEntity();
                            rol.IntIdRol = (int)dr.GetValue(0);
                            rol.VarNombre = (string)dr.GetValue(1);
                            rol.BitIsDeleted = (bool)dr.GetValue(2);

                            roles.Add(rol);
                        }
                    }
                }
                return roles;

            }catch(Exception ex){
                throw ex;
            }
        }

        /// <summary>
        /// Funcion que borra un rol
        /// </summary>
        /// <returns></returns>
        public bool DeleteRol(Entities.RolEntity rol)
        {
            try
            {
                using (SqlConnection conn = this.OpenConn())
                {

                    SqlCommand dCmd = new SqlCommand("MARAVILLA_SQL.spDeleteRol", conn);

                    dCmd.CommandText = "MARAVILLA_SQL.spDeleteRol";

                    dCmd.CommandType = CommandType.StoredProcedure;


                    dCmd.Parameters.AddWithValue("@intIdRol", rol.IntIdRol);

                    // dCmd.ExecuteNonQuery();
                    dCmd.ExecuteNonQuery();
                    dCmd.Dispose();
                }
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }




        /// <summary>
        /// Funcion que agrega un rol
        /// </summary>
        /// <returns></returns>
        public Entities.RolEntity AddRol(Entities.RolEntity rol)
        {
            try
            {
                using (SqlConnection conn = this.OpenConn())
                {

                    SqlDataReader dr;
                    SqlCommand dCmd = new SqlCommand("MARAVILLA_SQL.spAddRol", conn);

                    dCmd.CommandText = "MARAVILLA_SQL.spAddRol";

                    dCmd.CommandType = CommandType.StoredProcedure;


                    dCmd.Parameters.AddWithValue("@varNombre", rol.VarNombre);

                    // dCmd.ExecuteNonQuery();
                    dr = dCmd.ExecuteReader();
                    dCmd.Dispose();

                    dr.Read();

                    rol.IntIdRol = Int32.Parse(dr.GetValue(0).ToString());

                }
                return rol;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Funcion que actualiza un rol
        /// </summary>
        /// <returns></returns>
        public bool UpdateRol(Entities.RolEntity rol)
        {
            try
            {
                using (SqlConnection conn = this.OpenConn())
                {

                    SqlCommand dCmd = new SqlCommand("MARAVILLA_SQL.spUpdateRol", conn);

                    dCmd.CommandText = "MARAVILLA_SQL.spUpdateRol";

                    dCmd.CommandType = CommandType.StoredProcedure;


                    dCmd.Parameters.AddWithValue("@intIdRol", rol.IntIdRol);
                    dCmd.Parameters.AddWithValue("@varNombre", rol.VarNombre);

                    // dCmd.ExecuteNonQuery();
                    dCmd.ExecuteNonQuery();
                    dCmd.Dispose();
                }
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Funcion que busca los datos de un rol por su id
        /// </summary>
        /// <param name="_rol"></param>
        /// <returns></returns>
        public RolEntity GetRolById(RolEntity _rol)
        {
            try
            {
                RolEntity rol = new RolEntity();
                using (SqlConnection conn = this.OpenConn())
                {

                    SqlDataReader dr;

                    SqlCommand dCmd = new SqlCommand("MARAVILLA_SQL.spGetRolById", conn);

                    dCmd.CommandText = "MARAVILLA_SQL.spGetRolById";

                    dCmd.CommandType = CommandType.StoredProcedure;


                    dCmd.Parameters.AddWithValue("@intIdRol", _rol.IntIdRol);

                    // dCmd.ExecuteNonQuery();
                    dr = dCmd.ExecuteReader();
                    dCmd.Dispose();
                    dr.Read();

                    rol.IntIdRol = (int)dr.GetValue(0);
                    rol.VarNombre = (string)dr.GetValue(1);
                    rol.BitIsDeleted = (bool)dr.GetValue(2);
                }
                return rol;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
